#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tab->show();
    ui->tab_2->hide();
    ui->tab_3->hide();
    ui->listWidget->setAutoScroll(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void* DoConnect(void *athis)
{
    UserClient *t = (UserClient*)athis;
    t->ConnecttosServer();
}

void MainWindow::on_pushButton_clicked()
{
    user = new UserClient();
    user->parent = this;
    QString temp;
    string str;

    temp = ui->lineEdit->text();
    str = temp.toStdString();
    user->iUser.setUserName(str);

    temp = ui->lineEdit_2->text();
    str = temp.toStdString();
    user->iUser.setPassword(str);

    temp = ui->lineEdit_3->text();
    str = temp.toStdString();
    user->iUser.setHost(str);

    user->SetUserDetails(user->iUser);


    string j(user->iUser.getUserName());
    j.append("@");
    j.append(user->iUser.getHost());
    user->iUserJidString = j;
    JID j1(j);
    user->iUserJid = j1;

    pthread_create(&t, NULL,DoConnect,user);

    ui->tab->hide();
    ui->tab_2->show();

}

void MainWindow::on_pushButton_2_clicked()
{

    QString temp;
    string str;

    temp = ui->lineEdit_4->text();
    str = temp.toStdString();
    user->iDestUser.setUserName(str);

    temp = ui->lineEdit_5->text();
    str = temp.toStdString();
    user->iDestUser.setHost(str);

    user->SetDestinationUser(user->iDestUser);

    string j(user->iDestUser.getUserName());
    j.append("@");
    j.append(user->iDestUser.getHost());
    user->iDestJidString = j;
    JID j1(j);
    user->iDestJid = j1;

    ui->tab_2->hide();
    ui->tab_3->show();
 }

void MainWindow::on_lineEdit_6_returnPressed()
{
    QString temp, msg;
    string str, sep = "  :  ";

    temp = ui->lineEdit_6->text();
    str = temp.toStdString();

    if(user->iMessageSession == NULL)
    {
        MessageSession *s = new MessageSession(user->j, user->iDestJid);
        s->registerMessageHandler(user);
        s->send(str.c_str());
        user->iMessageSession = s;
    }
    else
    {
        user->iMessageSession->send(str.c_str());
    }

    ui->lineEdit_6->clear();
    msg.append(user->iUserJidString.c_str());
    msg.append(sep.c_str());
    msg.append(temp);
    ui->listWidget->addItem(msg);
    ui->listWidget->scrollToBottom();
}

void MainWindow::on_pushButton_3_clicked()
{
    QString temp, msg;
    string str, sep = "  :  ";

    temp = ui->lineEdit_6->text();
    str = temp.toStdString();

    if(user->iMessageSession == NULL)
    {
        MessageSession *s = new MessageSession(user->j, user->iDestJid);
        s->registerMessageHandler(user);
        s->send(str.c_str());
        user->iMessageSession = s;
    }
    else
    {
        user->iMessageSession->send(str.c_str());
    }

    ui->lineEdit_6->clear();
    msg.append(user->iUserJidString.c_str());
    msg.append(sep.c_str());
    msg.append(temp);
    ui->listWidget->addItem(msg);
    ui->listWidget->scrollToBottom();
}

void MainWindow::DisplayReceivedMessage(string s)
{
    string temp = "  :  ";
    QString t;

    t.append(user->iDestJidString.c_str());
    t.append(temp.c_str());
    t.append(s.c_str());
    ui->listWidget->addItem(t);
    ui->listWidget->scrollToBottom();
}
